package ubo.master1.jeupoker.gui;

public class LaCart implements Comparable<LaCart> {

    /** Nombre de carte Numero pris en compte. */
    public static final int NO_OF_RANKS = 13;
    /** nombre couleurs */
    public static final int NO_OF_SUITS = 4;
    // denomination des carte .
    public static final int ACE = 12;
    public static final int KING = 11;
    public static final int QUEEN = 10;
    public static final int JACK = 9;
    public static final int TEN = 8;
    public static final int NINE = 7;
    public static final int EIGHT = 6;
    public static final int SEVEN = 5;
    public static final int SIX = 4;
    public static final int FIVE = 3;
    public static final int FOUR = 2;
    public static final int THREE = 1;
    public static final int DEUCE = 0;
    // Les couleurs.
    public static final int SPADES = 3;
    public static final int HEARTS = 2;
    public static final int CLUBS = 1;
    public static final int DIAMONDS = 0;
    /* symbole pour les chifres */
    public static final String[] RANK_SYMBOLS = {
        "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"
    };
    /**  symbols pour les couleurs */
    public static final char[] SUIT_SYMBOLS = {'d', 'c', 'h', 's'};
    private final int rank;
    private final int suit;

    /**
     * Constructeur qui reçoit les deux paramettre couleur et chiffre.
     * 
     * @param rank
     *            Le chiffre .
     * @param suit
     *            La couleur.
     * 
     * @throws IllegalArgumentException
     *             si la numeru d'une colleur est invalide.
     */
    public LaCart(int rank, int suit) {
        if (rank < 0 || rank > NO_OF_RANKS - 1) {
            throw new IllegalArgumentException("Invalid rank");
        }
        if (suit < 0 || suit > NO_OF_SUITS - 1) {
            throw new IllegalArgumentException("Invalid suit");
        }
        this.rank = rank;
        this.suit = suit;
    }

    /**
     * Constructeur qui reçoit en entré le nom de la carte 
     * 
     * The string must consist of a rank character and a suit character, in that
     * order.
     * 
     * @param s
     *            Un string representant description de la carte , e.g. "As", "Td", "7h".
     * 
     * @return La carte.
     * 
     * @throws IllegalArgumentException
     *      Exception si la numero d'une colleur est invalide
     *             ou bien si la longueur de chaine dépasse 2 caractere.
     */
    public LaCart(String s) {
        if (s == null) {
            throw new IllegalArgumentException("Null string or of invalid length");
        }
        s = s.trim();
        if (s.length() != 2) {
            throw new IllegalArgumentException("Empty string or invalid length");
        }

        // Parser le numero de la carte .
        String rankSymbol = s.substring(0, 1);
        char suitSymbol = s.charAt(1);
        int rank = -1;
        for (int i = 0; i < LaCart.NO_OF_RANKS; i++) {
            if (rankSymbol.equals(RANK_SYMBOLS[i])) {
                rank = i;
                break;
            }
        }
        if (rank == -1) {
            throw new IllegalArgumentException("Unknown rank: " + rankSymbol);
        }
        // Parsser la couleur de la carte .
        int suit = -1;
        for (int i = 0; i < LaCart.NO_OF_SUITS; i++) {
            if (suitSymbol == SUIT_SYMBOLS[i]) {
                suit = i;
                break;
            }
        }
        if (suit == -1) {
            throw new IllegalArgumentException("Unknown suit: " + suitSymbol);
        }
        this.rank = rank;
        this.suit = suit;
    }

    /**
     * Returns la type de le couleur de la carte
     * 
     * @return La couleur.
     */
    public int getSuit() {
        return suit;
    }

    /**
     * Returns le numero de la carte .
     * 
     * @return Le numero de la carte.
     */
    public int getRank() {
        return rank;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return (rank * NO_OF_SUITS + suit);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof LaCart) {
            return ((LaCart) obj).hashCode() == hashCode();
        } else {
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(LaCart card) {
        int thisValue = hashCode();
        int otherValue = card.hashCode();
        if (thisValue < otherValue) {
            return -1;
        } else if (thisValue > otherValue) {
            return 1;
        } else {
            return 0;
        }
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return RANK_SYMBOLS[rank] + SUIT_SYMBOLS[suit];
    }
}